package com.hexacta.hrs.web.struts.actions.feedbacks;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.vectrics.employee.Employee;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;
import org.vectrics.user.Person;

import com.hexacta.hrs.domain.feedback.FeedbackPeriod;
import com.hexacta.hrs.domain.feedback.Period;
import com.hexacta.hrs.securitygroups.PermissionName;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.feedbacks.FeedbackPeriodService;
import com.hexacta.hrs.services.feedbacks.PeriodService;
import com.hexacta.hrs.services.mentoring.MentoringService;
import com.hexacta.hrs.web.struts.actions.security.PermissionChecker;
import com.opensymphony.xwork2.ActionSupport;

public class FeedbackFinalResultsAction extends ActionSupport implements SessionAware, ServletRequestAware {

    private static final long serialVersionUID = -637736393164256373L;

    private Map<String, Object> session;

    private FeedbackPeriodService feedbackPeriodService;

    private EmployeeService employeeService;

    private MentoringService mentoringService;

    private PeriodService periodService;

    private PermissionChecker permissionChecker = new PermissionChecker();

    private FeedbackFinalResultsFilters filters;

    private List<FeedbackPeriod> feedbackPeriods;

    private String EXPORT = "export";

    private InputStream reportStream;

    private boolean canExport;

    private HttpServletRequest request;

    public Map<String, Object> getSession() {
        return session;
    }

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    public String initFilters() {
        // this.filters = new FeedbackFinalResultsFilters();
        canExport = false;
        return SUCCESS;
    }

    public String filter() {

        canExport = true;
        Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(getSession());

        Period period = periodService.getPeriodByYear(filters.getPeriod());

        feedbackPeriods = (List<FeedbackPeriod>) feedbackPeriodService.findFeedbackPeriodsWithCriteria(period,
                person.getId(), filters.getSelectedEmployeeId(), filters.getSelectedRoleId(),
                filters.getSelectedMentorId());

        return SUCCESS;
    }

    public String export() throws Exception {

        if (!canExport) {
            addActionError("You have to filter first in order to export.");
            return SUCCESS;
        }

        Period period = periodService.getPeriodByYear(filters.getPeriod());

        Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(getSession());
        feedbackPeriods = (List<FeedbackPeriod>) feedbackPeriodService.findFeedbackPeriodsWithCriteria(period,
                person.getId(), filters.getSelectedEmployeeId(), filters.getSelectedRoleId(),
                filters.getSelectedMentorId());

        ResultsExporter exporter = new ResultsExporter(feedbackPeriods);

        exporter.build();

        reportStream = exporter.getReportStream();

        return EXPORT;
    }

    public boolean isOpen(){
    	return periodService.getPeriodByYear(filters.getPeriod()).isClosed();
    }
    
    /**
     * @return the evaluatedEmployees
     */
    public List<Employee> getEvaluatedEmployees() {
        return (List<Employee>) employeeService.getAllCurrentEmployees();
    }

    /**
     * @return the feedbacPeriodService
     */
    public FeedbackPeriodService getFeedbackPeriodService() {
        return feedbackPeriodService;
    }

    /**
     * @param feedbacPeriodService
     *            the feedbacPeriodService to set
     */
    public void setFeedbackPeriodService(final FeedbackPeriodService feedbackPeriodService) {
        this.feedbackPeriodService = feedbackPeriodService;
    }

    /**
     * @return the mentors
     */
    public List<Employee> getMentors() {
        return (List<Employee>) mentoringService.getAllActiveMentors();
    }

    /**
     * @return the roles
     */
    public List<JobFunction> getRoles() {
        return RecruitServiceLocator.getJobProvider().findAllJobFunctions();
    }

    /**
     * @return the employeeService
     */
    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    /**
     * @param employeeService
     *            the employeeService to set
     */
    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public PeriodService getPeriodService() {
        return periodService;
    }

    public void setPeriodService(final PeriodService periodService) {
        this.periodService = periodService;
    }

    /**
     * @return los feedbacks que responden al filtro.
     */

    public List<FeedbackPeriod> getFeedbackPeriods() {
        return feedbackPeriods;
    }

    /**
     * @return the mentoringService
     */
    public MentoringService getMentoringService() {
        return mentoringService;
    }

    /**
     * @param mentoringService
     *            the mentoringService to set
     */
    public void setMentoringService(final MentoringService mentoringService) {
        this.mentoringService = mentoringService;
    }

    public FeedbackFinalResultsFilters getFilters() {
        return filters;
    }

    public List<Employee> getAllEmployeesList() {
        return (List<Employee>) employeeService.getAllCurrentEmployees();
    }

    public List<JobFunction> getAllRoleList() {
        return RecruitServiceLocator.getJobProvider().findAllJobFunctions();
    }

    public List<Employee> getAllMentorsList() {
        return (List<Employee>) mentoringService.getAllActiveMentors();
    }

    public void setFilters(final FeedbackFinalResultsFilters filters) {
        this.filters = filters;
    }

    public void setFeedbackPeriods(final List<FeedbackPeriod> feedbackPeriods) {
        this.feedbackPeriods = feedbackPeriods;
    }

    /**
     * @return the reportStream
     */
    public InputStream getReportStream() {
        return reportStream;
    }

    /**
     * @param reportStream
     *            the reportStream to set
     */
    public void setReportStream(final InputStream reportStream) {
        this.reportStream = reportStream;
    }

    /**
     * @return the canExport
     */
    public boolean isCanExport() {
        return canExport;
    }

    /**
     * @param canExport
     *            the canExport to set
     */
    public void setCanExport(final boolean canExport) {
        this.canExport = canExport;
    }

    public String getContentDisposition() {
        return "attachment;filename=FinalResults.xlsx";
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
     * javax.servlet.http.HttpServletRequest)
     */
    @Override
    public void setServletRequest(final HttpServletRequest request) {
        this.request = request;
    }

    /**
     * @return the request
     */
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * @return the showReturn
     */
    public boolean isShowReturn() {
        String permission = PermissionName.MANAGE_FEEBACKS.toString();
        return permissionChecker.hasPermission(permission, request.getSession());
    }

}